// Harvester代码
var roleHarvester = {

    /** @param {Creep} creep **/
    run: function (creep) {
        // 进行判断，若背包未满，则采矿
        if (creep.store.getFreeCapacity() > 0) {

            var sources = creep.room.find(FIND_SOURCES); // 定义矿点

            // 判断，若矿点离爬较远，则向矿点移动
            if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { //注意，矿的位置以数组形式储存
                creep.moveTo(sources[0], { visualizePathStyle: { stroke: '#ffaa00' } }); //向矿点移动，路径可视化
            }
        }
        else {
            var targets = creep.room.find(FIND_STRUCTURES, { //找出需要补充能量的建筑
                filter: (structure) => {
                    return (structure.structureType == STRUCTURE_EXTENSION ||
                        structure.structureType == STRUCTURE_SPAWN ||
                        structure.structureType == STRUCTURE_TOWER) &&
                        structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
                }
            });
            if (targets.length > 0) { // 需要补充能量的建筑数目 > 0
                if (creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                    creep.moveTo(targets[0], { visualizePathStyle: { stroke: '#ffffff' } });//向目标建筑移动，路径可视化
                }
            }
        }
    }
};

module.exports = roleHarvester;
